#include  "vector.h"
#include  <cmath>
#include  <limits>

/*----------------------------------------------------------*/

const double Vector::norm_1() const
{
  const_iterator first  = std::vector<double>::begin();
  const_iterator last   = std::vector<double>::end();

  double norm(0);
  while( first != last)
  {
    norm += fabs( ( *first++ ) );
  }
  return norm;
}

/*----------------------------------------------------------*/

const double Vector::norm_2() const
{
  const_iterator first  = std::vector<double>::begin();
  const_iterator last   = std::vector<double>::end();

  double norm(0);
  if(first == last)
  {
    return norm;
  }
  while( first != last)
  {
    norm += ( ( *first ) )*( ( *first ) );
    first++;
  }
  return std::sqrt( static_cast<double>( norm ) );
}

/*----------------------------------------------------------*/

const double Vector::norm_infty() const
{
  const_iterator first  = std::vector<double>::begin();
  const_iterator last   = std::vector<double>::end();

  double d(0);
  while( first != last)
  {
    d = std::max( d, fabs(*first) );
    first++;
  }
  return d;
}

/*----------------------------------------------------------*/

void Vector::zero()
{
  iterator first  = std::vector<double>::begin();
  const_iterator last   = std::vector<double>::end();

  while( first != last)
  {
    *first++ = 0;
  }
}

/*----------------------------------------------------------*/

double Vector::max() const
{
  double d = std::numeric_limits<double>::min();
  const_iterator first  = std::vector<double>::begin();
  const_iterator last   = std::vector<double>::end();

  while( first != last)
  {
    d = std::max( d, ( *first++ ) );
  }
  return d;
}

/*----------------------------------------------------------*/

double Vector::min() const
{
  double d = std::numeric_limits<double>::max();
  const_iterator first  = std::vector<double>::begin();
  const_iterator last   = std::vector<double>::end();

  while( first != last)
  {
    d = std::min( d, ( *first++ ) );
  }
  return d;
}

/*----------------------------------------------------------*/

double Vector::operator* (const Vector& v) const
{
  const_iterator first  = std::vector<double>::begin();
  const_iterator last   = std::vector<double>::end();
  const_iterator first2 = v.begin();

  double d(0);
  while(first != last)
  {
    d += ( *first++ )*( *first2++ );
  }
  return d;
}

/*----------------------------------------------------------*/

void Vector::equal (const double d, const Vector& v)
{
  iterator first  = std::vector<double>::begin();
  const_iterator last   = std::vector<double>::end();
  const_iterator first2 = v.begin();

  while(first != last)
  {
    ( *first++ ) = d*( *first2++ );
  }
}

/*----------------------------------------------------------*/

void Vector::add (const double d, const Vector& v)
{
  iterator first  = std::vector<double>::begin();
  const_iterator last   = std::vector<double>::end();
  const_iterator first2 = v.begin();

  while(first != last)
  {
    ( *first++ ) += d*( *first2++ );
  }
}

